package com.tao.plan.controller;

import com.tao.plan.common.BaseThreadLocal;
import com.tao.plan.common.R;
import com.tao.plan.domain.pojo.PCommonPlan;
import com.tao.plan.domain.vo.CommonPlanCountVo;
import com.tao.plan.domain.vo.CommonPlanVo;
import com.tao.plan.service.PCommonPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/v1/commonPlan")
public class CommonPlanController {
	@Autowired
	private PCommonPlanService pCommonPlanService;

	/**
	 * 新增
	 */
	@PostMapping("/add")
	public R addCommonPlan(@RequestBody PCommonPlan pCommonPlan){
		// 直接新增
		pCommonPlan.setUid(Long.valueOf(BaseThreadLocal.getUid()));
		pCommonPlan.setUpdateTime(LocalDateTime.now());
		pCommonPlanService.save(pCommonPlan);
		return R.build().success();
	}

	/**
	 * 查询
	 * 基于计划的类型, 和当前状态
	 */
	@GetMapping("/list/{type}/{status}")
	public R list(@PathVariable("type") Integer type,@PathVariable("status") Integer status){
		List<PCommonPlan> list =  pCommonPlanService.listByTypeAndStatus(type,status);
		return R.build().success().put("data",list);
	}

	/**
	 * 删除
	 */
	@DeleteMapping("/delete/{id}")
	public R delete(@PathVariable("id") Long id){
		pCommonPlanService.removeById(id);
		return R.build().success();
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	public R update(@RequestBody PCommonPlan pCommonPlan){
		pCommonPlanService.updateById(pCommonPlan);
		return R.build().success();
	}

	/**
	 * 修改状态为已完成
	 */
	@PostMapping("/setStatus/{id}")
	public R setStatus(@PathVariable("id") Long id){
		PCommonPlan pCommonPlan = new PCommonPlan();
		pCommonPlan.setId(id);
		pCommonPlan.setStatus(1);
		pCommonPlanService.updateById(pCommonPlan);
		return R.build().success();
	}

	/**
	 * 统计各种任务的完成情况, 当日待办
	 */
	@GetMapping("/count")
	public R count(){
		CommonPlanCountVo commonPlanCountVo = pCommonPlanService.countDoingTask();
		return R.build().success().put("data",commonPlanCountVo);
	}
}
